from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException 
from tencentcloud.ssl.v20191205 import ssl_client, models
from tkinter import ttk
import datetime
import re,os
import tkinter as tk
import threading
import codecs
import tkinter.messagebox as messagebox

A = tk.Tk()
scn_w,scn_h=A.maxsize()
A.title('SSL')
A.geometry('570x230')
A.wm_resizable(False,False)
one = tk.Label(A,text="过期天数:")
two = tk.Label(A,text="账号选择:")
one.grid(row=0,column=0,columnspan=1,padx=1,pady=5)
two.grid(row=1,column=0,columnspan=1,padx=1,pady=5)
search_name = tk.Entry(A, width=18,show = None)
search_name.grid(row=0,column=1,columnspan=5,padx=1,pady=5)

a,b,c,m=[],[],[],0
if os.path.exists(os.getcwd()+"\\set_SSL.conf"):
    set_path = os.getcwd()+"\\set_SSL.conf"
else:
    hh=os.popen('dir "%s\\soft\\"'%str(os.getcwd())).readlines()
    for i in hh:
        if "Tencent_SSL" in i:
            set_path=os.getcwd()+"\\soft\\"+i[36:].strip()+"\\set_SSL.conf"

file=codecs.open(set_path,'r','utf-8',buffering=True)
dd = file.read()
qq = re.findall(r'<USER>(.*?)</USER>', dd, re.S)
for i in qq:
    a.append(i)
id = re.findall(r'<SecretId>(.*?)</SecretId>', dd, re.S)
for i in id:
    b.append(i)
key = re.findall(r'<SecretKey>(.*?)</SecretKey>', dd, re.S)
for i in key:
    c.append(i)

ID = tk.StringVar()  # #创建变量，便于取值
com = ttk.Combobox(A, width=16,textvariable=ID,state='readonly')  # #创建下拉菜单
com.grid(row=1,column=1,columnspan=5,padx=1,pady=5)  # #将下拉菜单绑定到窗体
com["value"] = a  # #给下拉菜单设定值
com.current(0)  # #设定下拉菜单的默认值为第0个
def xFunc(m):
    qq=com.get()
    word = re.compile(r'<USER>%s</USER>\r\n<SecretId>(.*?)</SecretId>\r\n<SecretKey>(.*?)</SecretKey>' % qq)
    #print(com.get())  # #获取选中的值方法1
    #print(xVariable.get())  # #获取选中的值方法2
    if m == 1:
        return word.findall(dd)[0][0]
    if m == 2:
        return word.findall(dd)[0][1]



com.bind("<<ComboboxSelected>>", xFunc)  # #给下拉菜单绑定事件
def sss():
    B.config(state='normal')
    B.delete(0.0, 'end')
    dd = search_name.get()
    def select():
        try:
            cred = credential.Credential(xFunc(1),xFunc(2))
            httpProfile = HttpProfile()
            httpProfile.endpoint = "ssl.tencentcloudapi.com"
            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = ssl_client.SslClient(cred, "", clientProfile)
            req = models.DescribeCertificatesRequest()
            params = '{\"Limit\":99999}'
            req.from_json_string(params)

            resp = client.DescribeCertificates(req)
            return resp.to_json_string()

        except TencentCloudSDKException as err:
            print(err)
    html=select()
    n=0
    dl_name = re.findall(r'"Domain": (.*?), "Alias"', html, re.S)
    for dl_name in dl_name:
        dl_name = re.findall(r'"(.*?)"', dl_name, re.S)
        #dl_start_time = re.findall(r'"CertBeginTime": "(.*?)"', html, re.S)[n]
        dl_end_time = re.findall(r'"CertEndTime": "(.*?)"', html, re.S)[n]
        dl_end_time = datetime.datetime.strptime(dl_end_time,'%Y-%m-%d %H:%M:%S')
        day = (dl_end_time-datetime.datetime.now()).days
        if  day < int(dd) and day > -3:
            var= ('证书:'+str(dl_name[0])+' <---------> '+'剩余时长:'+str(day)+'天'+'\n')
            B.insert('end',var)
            B.see('end')
            n += 1
        else:
            n += 1
    B.config(state='disabled')
def thread_it(func):
    t = threading.Thread(target=func)
    t.setDaemon(True)
    t.start()



b2 = tk.Button(A, text='查找', width=5,height=1, command=lambda :thread_it(sss))
b2.grid(row=1,column=5,columnspan=6,padx=2,pady=1)
b = tk.Label(A,text="查找结果:")
b.grid(row=2,column=0,columnspan=1,padx=1,pady=10)
B = tk.Text(A, width=69,height=10)
B.grid(row=2,column=2,columnspan=18,padx=1,pady=10)
B.config(state='disabled')
D = tk.Label(A,)
# 第8步，主窗口循环显示
A.mainloop()
